<template>
  <div>
    <i-dialog
      :title="'添加设备'"
      :visible="visible"
      width="642px"
      :close-on-click-modal="false"
      @close="outerClose"
    >
      <i-form
        :items="formItems"
        :model="form"
        ref="form"
        :rules="rules"
        label-width="95px"
        class="demo-form"
      >
      </i-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outerClose">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </span>
    </i-dialog>
  </div>
</template>
<script>
import { activateDevice } from '@/api/device'
import formValidate from '@/utils/validate'
export default {
  name: 'AddDevice',
  data() {
    return {
      form: {
        stationName: this.stationAttr.stationName,
        stationAddress: this.stationAttr.stationAddress,
      },
      formItems: [
        {
          type: 'detail',
          value: 'stationName',
          label: '充电站',
          clearable: true,
        },
        {
          type: 'detail',
          value: 'stationAddress',
          label: '充电站地址',
          clearable: true,
        },
        {
          type: 'text',
          value: 'deviceId',
          label: this.stationAttr.type === 'GATEWAY' ? 'IMEI号' : '设备编号',
          clearable: true,
        },
        {
          type: 'text',
          value: 'label',
          label: '设备标签',
          clearable: true,
        },
        {
          type: 'text',
          value: 'mchName',
          label: '厂商',
          clearable: true,
          ifRender: () => {
            return this.stationAttr.type == 'CENTRAL_SOCKET'
          },
        },
        {
          type: 'text',
          value: 'socketNum',
          label: '端口数',
          clearable: true,
          ifRender: () => {
            return this.stationAttr.type == 'CENTRAL_SOCKET'
          },
        },
        {
          value: 'partnerIds',
          label: '分账人',
          clearable: true,
          type: 'select',
          options: this.partnerOptions,
        },
      ],
      rules: {
        deviceId: [
          {
            required: true,
            message: '不能为空',
          },
          {
            max: 16,
            message: '最多允许输入16个字符',
            trigger: ['change', 'blur'],
          },
          // {
          //   validator: formValidate('vaildateInteger'),
          //   trigger: ['change', 'blur'],
          // },
        ],
        label: [
          {
            max: 18,
            message: '最多允许输入18个字符',
            trigger: ['change', 'blur'],
          },
        ],
        mchName: [
          {
            required: true,
            max: 20,
            message: '最多允许输入20个字符',
            trigger: ['change', 'blur'],
          },
          {
            validator: formValidate('validateExplanation'),
            trigger: ['change', 'blur'],
          },
        ],
        socketNum: [
          {
            required: true,
            message: '请输入端口数',
          },
          {
            validator: formValidate('vaildateInteger'),
            trigger: ['change', 'blur'],
          },
        ],
      },
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    stationAttr: {
      type: Object,
      default: () => {},
    },
    partnerOptions: {
      type: Array,
      default: () => [],
    },
  },
  mounted() {
    console.log(123456)
  },
  methods: {
    outerClose() {
      this.$emit('update:visible', false)
    },
    refreshTable() {
      this.$emit('refreshTable')
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let params = {
            deviceIds: [this.form.deviceId],
            labels: [this.form.label],
            stationId: this.stationAttr.id,
            type: this.stationAttr.type,
            socketNum: [this.form.socketNum],
            mchName: [this.form.mchName],
            partnerIds: [this.form.partnerIds],
          }
          activateDevice(params).then(
            (res) => {
              if (res.successNum === res.total) {
                this.$message({
                  message: '添加设备成功',
                  type: 'success',
                })
              } else {
                this.$message({
                  message: res.fail.join(',') || '添加设备失败',
                  type: 'error',
                })
              }
              this.refreshTable()
              this.outerClose()
            },
            () => {
              this.refreshTable()
              this.outerClose()
            }
          )
        }
      })
    },
  },
}
</script>
